package com.yz.crm.employee.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import com.yz.common.model.PageResult;
import com.yz.common.security.bcrypt.BCrypt;
import com.yz.common.sensitive.util.EmojiUtil;
import com.yz.common.util.IDUtils;
import com.yz.crm.db.dao.BaseEmployeeMapper;
import com.yz.crm.db.model.BaseCompany;
import com.yz.crm.db.model.BaseEmployee;
import com.yz.crm.db.model.BaseEmployeeExample;
import com.yz.crm.db.model.BaseRole;
import com.yz.crm.employee.model.Employee;
import com.yz.crm.employee.model.EmployeeSearchBean;
import com.yz.crm.employee.service.EmployeeService;
import com.yz.crm.system.constant.SpecialRoleEnum;
import com.yz.crm.system.service.RoleService;
/**
 * 员工服务实现类
 * */
@Service
public class EmployeeServiceImpl implements EmployeeService {

	@Resource
	private SqlSession sqlSession;
	
	@Resource
	private RoleService roleService;
	
	@Override
	public PageResult<Employee> listEmployee(EmployeeSearchBean bean) {
		int count = sqlSession.selectOne("com.yz.crm.db.dao.BaseEmployeeMapper.countByEx", bean);
		bean.setTotalCount(count);
		List<Employee> result = sqlSession.selectList("com.yz.crm.db.dao.BaseEmployeeMapper.selectByEx", bean);
		bean.setResult(result);
		bean.update();
		return bean;
	}

	@Override
	public BaseEmployee get(String id) {
		return sqlSession.getMapper(BaseEmployeeMapper.class).selectByPrimaryKey(id);
	}
	
	@Override
	public BaseEmployee getByUsername(String username, String password) {
		
		BaseEmployeeExample example = new BaseEmployeeExample();
		example.createCriteria().andUsernameEqualTo(username).andIsValidEqualTo(true);
		
		BaseEmployeeMapper mapper = sqlSession.getMapper(BaseEmployeeMapper.class);
		List<BaseEmployee> list = mapper.selectByExample(example);
		
		if (list == null || list.size() == 0) {
			return null;
		}
		
		BaseEmployee employee = list.get(0);
		boolean matched = BCrypt.checkpw(password, employee.getPassword());
		if (!matched) {
			return null;
		}
		return employee;
		
	}

	@Override
	public boolean saveOrUpdate(BaseEmployee employee) {
		
		String securedPassword = BCrypt.hashpw(employee.getPassword(),
				BCrypt.gensalt(12));
		employee.setPassword(securedPassword);
		
		if (employee.getId()==null || employee.getId().isEmpty()){
			employee.setId(IDUtils.getUUID());
			employee.setCreateDate(new Date());
			employee.setModifyDate(employee.getCreateDate());
			employee.setIsValid(true);
			return sqlSession.getMapper(BaseEmployeeMapper.class).insert(employee) > 0;
		}else{
			employee.setModifyDate(new Date());
			return sqlSession.getMapper(BaseEmployeeMapper.class).updateByPrimaryKey(employee) > 0;
		}
	}
	
	
	@Override
	public BaseEmployee createCompanyAdmin(BaseCompany company) {
		
		BaseEmployee employee = new BaseEmployee();
		employee.setCompanyId(company.getId());
		employee.setCreatorName("system");
		employee.setName(company.getName());
		employee.setUsername(company.getUsername());
		employee.setPassword(company.getPassword());
		
		saveOrUpdate(employee);
		
		BaseRole companyAdmin = roleService.getRole(company.getId(), 
				SpecialRoleEnum.CompanyAdmin.getCode());
		
		if (companyAdmin==null){
			companyAdmin = SpecialRoleEnum.CompanyAdmin.createRole();
			companyAdmin.setCompanyId(company.getId());
			roleService.save(companyAdmin);
		}
		
		roleService.addEmployeeRole(employee.getId(), companyAdmin.getId());
		
		return employee;
	}

	@Override
	public boolean delete(String id) {
		BaseEmployee be = get(id);
		if (be!=null){
			be.setIsValid(false);
			return saveOrUpdate(be);
		}
		return false;
	}

}
